package com.tang.dict;

import com.alibaba.fastjson.JSONObject;
import com.tang.system.mapper.DictParserMapper;
import com.tang.utils.CommonUtils;
import com.tang.utils.LazyAutowired;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.List;

public abstract class MergeDictParse extends DictParseAble {
    @LazyAutowired
    DictParserMapper dictParserMapper;

    protected void execute(DictParseField field) {
        if (field.getType() == DictType.dictCode) {
            executeDictCode((MergedDictParseField) field);
        }
        if (field.getType() == DictType.dictTable) {
            executeDictTable((MergedDictParseField) field);
        }
        if (field.getType() == DictType.dictTableObject) {
            executeDictTableObject((MergedDictParseField) field);
        }
    }

    private void executeDictTableObject(MergedDictParseField field) {
        String[] values = getFieldValues(field.getDictValue());
        if (CommonUtils.isEmpty(values)) return;
        String dictTable = field.getDictTable();
        String dictKeyColumn = field.getDictKeyColumn();
        List<JSONObject> list = dictParserMapper.queryByDictTableObject(dictTable, dictKeyColumn, values);
        if (CommonUtils.isEmpty(list)) return;
        List objectList = CommonUtils.newList();
        for (JSONObject jsonObject : list) {
            objectList.add(jsonObject.toJavaObject(field.getResultType()));
        }
        field.setDictObjects(objectList);
        field.callback();
    }

    private void executeDictTable(MergedDictParseField field) {
        String[] values = getFieldValues(field.getDictValue());
        if (CommonUtils.isEmpty(values)) return;
        String dictTable = field.getDictTable();
        String dictKeyColumn = field.getDictKeyColumn();
        String dictTitleColumn = field.getDictTitleColumn();

        List<String> titles = dictParserMapper.queryByDictTable(dictTable, dictKeyColumn, dictTitleColumn, Arrays.asList(values));
        if (CommonUtils.isEmpty(titles)) return;
        String titleString = String.join(",", titles);
        field.setDictTitle(titleString);
        field.callback();
    }

    private void executeDictCode(MergedDictParseField field) {
        String[] values = getFieldValues(field.getDictValue());
        if (CommonUtils.isEmpty(values)) return;
        String dictCode = field.getDictCode();
        List<String> titles = dictParserMapper.queryByDictCode(dictCode, Arrays.asList(values));
        if (CommonUtils.isEmpty(titles)) return;
        String titleString = String.join(",", titles);
        field.setDictTitle(titleString);
        field.callback();
    }

    private String[] getFieldValues(String dictValue) {
        String[] split = dictValue.split(",");
        for (int i = 0; i < split.length; i++) {
            if("true".equals(split[i])) split[i] = "1";
            if("false".equals(split[i])) split[i] = "0";
        }
        return split;
    }

    protected List<DictParseField> mergeFields(List<DictParseField> dictParseFields) {
        List<DictParseField> fields = CommonUtils.newList();
        for (DictParseField dictParseField : dictParseFields) {
            boolean flag = true;
            for (DictParseField field : fields) {
                if (field.eq(dictParseField)) {
                    ((MergedDictParseField)field).getChildren().add(dictParseField);
                    flag = false;
                }
            }
            if (flag) {
                fields.add(createMergedDictParseField(dictParseField, dictParseField));
            }
        }

        return fields;
    }

    private MergedDictParseField createMergedDictParseField(DictParseField dictParseField, DictParseField parseField) {
        MergedDictParseField res = new MergedDictParseField();
        BeanUtils.copyProperties(dictParseField, res);
        res.getChildren().add(dictParseField);
        return res;
    }
}
